package com.example.questionnaire.controller;

import com.example.questionnaire.common.ServerResponse;
import com.example.questionnaire.exception.RException;
import com.example.questionnaire.service.AppidAndSecretService;
import com.example.questionnaire.service.QuestionnaireSettingService;
import com.example.questionnaire.service.QuestionnaireViewService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;



@RestController
@RequestMapping("/QuestionnaireViewController")
@Api(tags = "问卷外观")
public class QuestionnaireViewController{

    @Autowired
    private QuestionnaireViewService questionnaireViewService;
    @Autowired
    AppidAndSecretService appidAndSecretService;

    /**
     * @param formid（问卷id）
     * @param toEnableTheCover（是否开启封面）
     * @param img（封面图片）
     * @param btnText（答题按钮文案）
     * @param backgroundColor（答题按钮背景色）
     * @param isActivitiesThat（是否开启活动说明按钮）
     * @param activitiesBtnBgColor（活动说明按钮背景色）
     * @param graphicPageLinks（活动按钮跳转的图文页链接）
     * @param customButton（是否开启自定义按钮）
     * @param customButtonColor（自定义按钮颜色）
     * @param customButtonUrl（自定义按钮链接）
     * @param bgImg（答题背景图）
     * @param submitBtnColor（提交按钮颜色）
     * @param titleColor（标题颜色）
     * @param topicDryColor（题干颜色）
     * @param textSize（选项文字大小）
     * @param titleSize（题干文字大小）

     */
    @PostMapping("/SetTheAppearanceOfTheQuestionnaire")
    @ApiOperation(value = "问卷外观", response = ServerResponse.class, httpMethod = "POST")
    @Transactional(rollbackFor = {Exception.class, RuntimeException.class, RException.class})
    public ServerResponse SetTheAppearanceOfTheQuestionnaire(@RequestParam String formid,
                                                             @RequestParam boolean toEnableTheCover,
                                                             @RequestParam String img,
                                                             @RequestParam String btnText,
                                                             @RequestParam String backgroundColor,
                                                             @RequestParam boolean isActivitiesThat,
                                                             @RequestParam String activitiesBtnBgColor,
                                                             @RequestParam String graphicPageLinks,
                                                             @RequestParam boolean customButton,
                                                             @RequestParam String customButtonColor,
                                                             @RequestParam String customButtonUrl,
                                                             @RequestParam String bgImg,
                                                             @RequestParam String submitBtnColor,
                                                             @RequestParam String titleColor,
                                                             @RequestParam String topicDryColor,
                                                             @RequestParam String textSize,
                                                             @RequestParam String titleSize) throws ParseException {

        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String token = request.getHeader("token");
        if(appidAndSecretService.tokenTimeout(token)){
            String msg = "登录已过期";
            return ServerResponse.createBySuccess(10010,msg, null);

        }

//        String filename = "img/";
//        String ImageName = getRandomPassword();
//        ImageUpload(img, formid, filename, ImageName);
//
//        String filename1 = "bgImg/";
//        String ImageName1 = getRandomPassword();
//        ImageUpload(bgImg, formid, filename1, ImageName1);
        boolean b = questionnaireViewService.SetTheAppearanceOfTheQuestionnaire(formid, toEnableTheCover, img, btnText, backgroundColor, isActivitiesThat, activitiesBtnBgColor, graphicPageLinks, customButton, customButtonColor, customButtonUrl, bgImg, submitBtnColor, titleColor, topicDryColor, textSize, titleSize);
        if (b) {
            return ServerResponse.createBySuccess("保存成功");
        }
        return ServerResponse.createByError("保存失败");
    }


    /**
     * @param formid（问卷id）
     */
    @GetMapping("/GetTheAppearanceOfTheQuestionnaire")
    @ApiOperation(value = "获取问卷外观信息", response = ServerResponse.class, httpMethod = "GET")
    @Transactional(rollbackFor = {Exception.class, RuntimeException.class, RException.class})
    public ServerResponse SetTheAppearanceOfTheQuestionnaire(@RequestParam String formid) throws ParseException {

        return ServerResponse.createBySuccess(questionnaireViewService.getTheAppearanceOfTheQuestionnaire(formid));

    }

}
